package com.lancoo.answer.view.dialog

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import com.lancoo.answer.R
import com.lancoo.answer.databinding.EvViewDialogLoadingBinding


/**
 * todo 普通loading菊花带提示文字弹窗
 */
class DialogLoading(
    context: Context?
) : Dialog(context!!, R.style.CustomDialog) {

    private val keylistener =
        DialogInterface.OnKeyListener { dialog, keyCode, event ->
            keyCode == KeyEvent.KEYCODE_BACK && event.repeatCount === 0
        }

    private lateinit var binding: EvViewDialogLoadingBinding
    private var message: String = ""

    /**
     * 提交按钮
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = EvViewDialogLoadingBinding.inflate(layoutInflater)
        setContentView(binding.root)
//        setContentView(R.layout.view_dialog_loading)
        window!!.setGravity(Gravity.CENTER) //设置弹窗右对齐
        window!!.setWindowAnimations(R.style.MyDialogScale) //设置动画效果
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false)

        //让Dialog在用户按下返回键也不消失(根据需求谨慎添加)
        //setOnKeyListener(keylistener)
    }

    override fun show() {
        super.show()
        binding.TvDialogMessage.text = message
        if(message.trim().isNullOrEmpty()){
            binding.TvDialogMessage.visibility = View.GONE
        }else{
            binding.TvDialogMessage.visibility = View.VISIBLE
        }
    }

    fun setMessage(message: String) {
        this.message = message
    }
}